/**
 * @param {number[]} nums
 * @param {number} k
 * @return {number}
 */
 var minimalKSum = function(nums, k) {
  nums.sort((a,b) => a-b)
  let cnt = 0
  let sum = 0n
  sum += BigInt((1+nums[0]-1)*(nums[0]-1)/2)
  cnt += nums[0]-1
  for(let i = 1; i<nums.length && cnt < k; i++) {
    if(nums[i] - nums[i-1] > 1) {
      let need = Math.min(nums[i]-nums[i-1]-1, k-cnt)
      sum += (BigInt(nums[i-1]+ 1) + BigInt(nums[i-1] + 1 + need-1))*BigInt(need)/2n
      cnt += need
    }
  }
  let x = BigInt(nums[nums.length-1]+1)
  if(cnt < k) {
    sum += (x + x+BigInt(k-cnt)-1n) * BigInt(k-cnt)/2n
  }
  console.log(sum)
  return BigInt(sum)
};

//1983286874545